import Foundation

extension UICollectionView {
    
    func register<T: UICollectionViewCell>(cellClass type: T.Type) {
        return register(type, forCellWithReuseIdentifier: String(describing: type))
    }
    
    func register<T: UICollectionReusableView>(headerClass type: T.Type) {
        register(type, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: String(describing: type))
    }
    
    func register<T: UICollectionReusableView>(headerNib type: T.Type) {
        let nib = UINib(nibName: String(describing: type), bundle: nil)
        register(nib, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: String(describing: type))
    }
    
    func register<T: UICollectionReusableView>(footerClass type: T.Type) {
        register(type, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: String(describing: type))
    }
    
    func register<T: UICollectionReusableView>(footerNib type: T.Type) {
        let nib = UINib(nibName: String(describing: type), bundle: nil)
        register(nib, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: String(describing: type))
    }
    
    func register<T: UICollectionViewCell>(cellNib type: T.Type) {
        let nib = UINib(nibName: String(describing: type), bundle: Bundle(for: type))
        register(nib, forCellWithReuseIdentifier: String(describing: type))
    }
    
    func dequeueReusableCell<T: UICollectionViewCell>(_ type:T.Type, indexPath : IndexPath) -> T {
        return dequeueReusableCell(withReuseIdentifier: String(describing: type), for: indexPath) as! T
    }
    
    func dequeueReusableViewHeader<T: UICollectionReusableView>(_ type:T.Type, indexPath : IndexPath) -> T {
        return dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: String(describing: type), for: indexPath) as! T
    }
    
    func dequeueReusableViewFooter<T: UICollectionReusableView>(_ type:T.Type, indexPath : IndexPath) -> T {
        return dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: String(describing: type), for: indexPath) as! T
    }
}

